;;; Commentary:
;;;
;;; This file contains some variable definitions and functions that I use for publishing my site.


(setq-default org-src-fontify-natively t)	;syntax highlighting in org-modesource blocks

(setq my-html-preamble
      "<header id=\"banner\">
  <h1><a href=\"/home.html\">Kevin \"The Nuclear\" Bloom</a></h1>
  <hr />
  <nav><ul>
    <li><a href=\"/contact.html\">Contact</a></li>
    <li><a href=\"/blog/blog.html\">Blog</a></li>
    <li><a href=\"/projects.html\">Projects</a></li>
    <li><a href=\"/about-me.html\">About Me</a></li>
  </ul></nav>
</header>")

(setq org-html-head
      (with-temp-buffer
        (let ((css-dir (file-name-as-directory "~/org-site/www/styles/"))
              (css-files '("main.css")))
          (insert "<style type=\"text/css\">\n")
          (dolist (file css-files)
            (insert-file-contents (concat css-dir file)))
          (insert "</style>")
          (buffer-string))))

(setq my-blog-extra-head
      (concat
       "<link rel='stylesheet' href='/../styles/main.css' />"))

(defun insert-css ()
  (let* ((css-dir (expand-file-name (plist-get project-plist :publishing-directory)))
         (css-files (directory-files css-dir t "^.*\\.css$")))
    (dolist (file css-files)
      (with-temp-buffer
        (insert-file-contents file)
        (write-file file)))))

(defun my-blog-get-preview (file)
  "The comments in FILE have to be on their own lines, prefereably before and after paragraphs.
Written by Dennis Ogbe, modified by Kevin Bloom."
  (with-temp-buffer
    (insert-file-contents file)
    (goto-char (point-min))
    (let ((beg (+ 1 (re-search-forward "^#\\+BEGIN_PREVIEW$")))
          (end (progn (re-search-forward "^#\\+END_PREVIEW$")
                      (match-beginning 0))))
      (buffer-substring beg end))))

(defun my-blog-sitemap (project &optional sitemap-filename)
  "Generate the sitemap for my blog. Written by Dennis Ogbe, modified by Kevin Bloom."
  (let* ((project-plist (cdr project))
         (dir (file-name-as-directory
               (plist-get project-plist :base-directory)))
         (localdir (file-name-directory dir))
         (exclude-regexp (plist-get project-plist :exclude))
         (files (nreverse
                 (org-publish-get-base-files project exclude-regexp)))
         (sitemap-filename (concat dir (or sitemap-filename "sitemap.org")))
         (sitemap-sans-extension
          (plist-get project-plist :sitemap-sans-extension))
         (visiting (find-buffer-visiting sitemap-filename))
         file sitemap-buffer)
    (with-current-buffer
        (let ((org-inhibit-startup t))
          (setq sitemap-buffer
                (or visiting (find-file sitemap-filename))))
      (erase-buffer)
      ;; loop through all of the files in the project
      (while (setq file (pop files))
        (let ((fn (file-name-nondirectory file))
              (link ;; changed this to fix links. see postprocessor.
               (file-relative-name file (file-name-as-directory
                                         (expand-file-name (concat (file-name-as-directory dir) "..")))))
              (oldlocal localdir))
          (when sitemap-sans-extension
            (setq link (file-name-sans-extension link)))
          ;; sitemap shouldn't list itself
          (unless (equal (file-truename sitemap-filename)
                         (file-truename file))
            (let (;; get the title and date of the current file
                  (title (org-publish-format-file-entry "%t" file project-plist))
                  (date (org-publish-format-file-entry "%d" file project-plist))
                  ;; get the preview section from the current file
                  (preview (my-blog-get-preview file))
                  (regexp "\\(.*\\)\\[\\([^][]+\\)\\]\\(.*\\)"))
              ;; insert a horizontal line before every post, kill the first one
              ;; before saving
              (insert "-----\n")
              (cond ((string-match-p regexp title)
                     (string-match regexp title)
                     ;; insert every post as headline
                     (insert (concat"* " (match-string 1 title)
                                    "[[file:" link "]["
                                    (match-string 2 title)
                                    "]]" (match-string 3 title) "\n")))
                    (t (insert (concat "* [[file:" link "][" title "]]\n"))))
              ;; add properties for `ox-rss.el' here
              (let ((rss-permalink (concat (file-name-sans-extension link) ".html"))
                    (rss-pubdate (format-time-string
                                  (car org-time-stamp-formats)
                                  (org-publish-find-date file))))
                (org-set-property "RSS_PERMALINK" rss-permalink)
                (org-set-property "PUBDATE" rss-pubdate))
              ;; insert the date, preview, & read more link
              (insert (concat date "\n\n"))
              (insert preview)
              (let ((new-link (reduce (lambda (x y) (concat x "/" y)) (cdr (split-string link "/")))))
                (insert (concat "[[file:" new-link "][Read More...]]\n")))))))
      ;; kill the first hrule to make this look OK
      (goto-char (point-min))
      (let ((kill-whole-line t)) (kill-line))
      (save-buffer))
    (or visiting (kill-buffer sitemap-buffer))))

(setq org-publish-project-alist
      `(("site" :components ("main" "main-static" "blogs" "styles"))
        ("main"
         :base-directory "~/personal/src/org-site/"
         :base-extension "org"
         :publishing-directory "~/personal/src/org-site/www/"
         :recursive t
         :publishing-function org-html-publish-to-html
         :headline-levels 4             ; Just the default for this project.
         :section-numbers nil
         :with-toc nil
         :with-drawers t
         :with-sub-superscript nil ;; important!!

         :html-link-home "/"
         :html-head nil ;; cleans up anything that would have been in there.
         :html-head-extra ,my-blog-extra-head
         :html-head-include-default-style nil
         :html-head-include-scripts nil
         :html-viewport nil
         :html-home/up-format ""
         :html-link-up ""
         :html-link-home ""
         ;; :auto-preamble t
         :html-postamble nil
         :html-preamble ,my-html-preamble
         )
        ("blogs"
         :base-directory "~/personal/src/org-site/blog"
         :base-extension "org"
         :publishing-directory "~/personal/src/org-site/www/blog"
         :recursive t
         :publishing-function org-html-publish-to-html
         :headline-levels 4             ; Just the default for this project.
         :section-numbers nil
         :with-toc nil
         :with-drawers t
         :with-sub-superscript nil ;; important!!

         :html-link-home "/"
         :html-head nil ;; cleans up anything that would have been in there.
         :html-head-extra ,my-blog-extra-head
         :html-head-include-default-style nil
         :html-head-include-scripts nil
         :html-viewport nil
         :html-home/up-format ""
         :html-link-up ""
         :html-link-home ""
         ;; :auto-preamble t
         :html-postamble nil
         :html-preamble ,my-html-preamble

         ;; sitemap - list of blog articles
         :auto-sitemap t
         :sitemap-filename "blog.org"
         :sitemap-title "Blog"

         :title "Blog Posts"
         ;; custom sitemap generator function
         :sitemap-function my-blog-sitemap
         :sitemap-sort-files anti-chronologically
         :sitemap-date-format "Published: %a %b %d %Y"
         )
        ("main-static"
         :base-directory "~/personal/src/org-site/"
         :base-extension "css\\|js\\|png\\|jpg\\|gif\\|pdf\\|mp3\\|ogg\\|swf"
         :publishing-directory "~/personal/org-site/www/"
         :recursive t
         :publishing-function org-publish-attachment
         )
        ("styles"
         :base-directory "~/personal/src/org-site/styles"
         :base-extension ".*"
         :publishing-directory "~/personal/src/org-site/www/styles/"
         :publishing-function org-publish-attachment
         ;; :completion-function insert-css
         )
        ))
